﻿using System;
using System.Web;
using RaceList.activity.util;

namespace RaceList.activity.help
{
    public static class HtmlHelp
    {
        /// <summary>
        ///     将根据字符串最大长度来判断截取多少才是合理的
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length">想要截取的长度</param>
        /// <returns></returns>
        public static string SubHtml(this string str, int length)
        {
            int len = str.Length;
            if (len > length)
                return str.Substring(0, length);
            return str.Substring(0, len);
        }

        /// <summary>
        ///     主要解决微信中缓存的问题，添加上一个timestamp的参数
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string AddTimeStamp(this string url)
        {
            if (url.IndexOf('?') > 0)
                return url + "&sh=" + DateTime.Now.Ticks;
            return "?sh=" + DateTime.Now.Ticks;
        }

        public static void Redirect404(this HttpResponse response, string referer)
        {
            response.Redirect("404.aspx?" + Parameters.referer + "=" + referer, true);
        }
    }
}